package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.util.DateUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.awt.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("supplier")
public class SupplierController {


    @Autowired
    SupplierService supplierService;

//分页查询供应商
    /**
     * 请求URL：http://localhost:8080/supplier/list
     * 请求参数：Integer page（当前页数）, Integer rows（每页显示的记录数）, String supplierName
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("list")
    public Map<String,Object> list(Integer page,Integer  rows,String supplierName){
        HashMap hashMap=new HashMap();
             ArrayList<Supplier> list=supplierService.list(page,rows,supplierName);
   hashMap.put("total",list.size());
   hashMap.put("rows",list);
         return  hashMap;
    }



    //供应商添加或修改
    /**
     * 请求URL：http://localhost:8080/supplier/save?supplierId=1
     * 请求参数：Supplier supplier
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：ServiceVO
     */

    @PostMapping("/save")
    public ServiceVO save(HttpServletRequest request, Supplier supplier){
        String supplierId = request.getParameter("supplierId");
        System.out.println(supplierId);
        if (supplierId==null){
                    //是保存
                      supplierService.save(supplier);
                      return  new ServiceVO(100,"请求成功",null);
                }else{
                    //是修改
                    supplier.setSupplierId(Integer.parseInt(supplierId.toString()));
                    supplierService.update(supplier);
                    return  new ServiceVO(100,"请求成功",null);
                }
    }


     //删除
    /**
     * 请求URL：http://localhost:8080/supplier/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */

        @PostMapping("delete")
      public ServiceVO delete(Integer ids){
            System.out.println(ids);
            supplierService.delete(ids);
     return  new ServiceVO(100,"请求成功",null);
      }



//供应商下拉列表查询
    /**
     * 请求URL：http://localhost:8080/supplier/getComboboxList
     * 请求参数：String q（供应商名称模糊查询）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：List<Supplier>
     */

    @PostMapping("getComboboxList")
    public  List<Supplier> getComboboxList(String q){
        List<Supplier> list=supplierService.getComboboxList(q);
          return  list;
    }











}
